<?php

namespace application\controllers;

use application\BaseController;
use fastphp\facade\Db;
use fastphp\facade\Request;
use fastphp\facade\Session;
use fastphp\util\Directory;
use fastphp\util\Header;

class AdminUserController extends BaseController
{
    public function login()
    {
        if (Request::isGet()) {
            if (!is_file(ROOT . 'token/installed.txt')){
                Header::setLocation('../../install.php');
                exit();
            }
           $userPwd = Session::get('userPwd');
           $userName = Session::get('userName');
            if ($userPwd) {
                $this->set('userName', $userName);
                $this->set('userPwd', $userPwd);
            }
            $this->_view->render();
        } else if (Request::isPost()) {
            $param = Request::post();
            if (empty($param['account'])) {
                $this->json('用户名不能为空', null, 400);
                exit();
            }
            if (empty($param['password'])) {
                $this->json('密码不能为空', null, 400);
                exit();
            }

            if (empty($param['code']) || Session::get('captcha') != $param['code']) {
                $this->json('验证码错误', null, 400);
                exit();
            }
            if (!empty($param['remember'])) {
                Session::set('userPwd', $param['password']);
                Session::set('userName', $param['account']);
            }else{
                if (Session::get('userName') == $param['account']){
                    Session::clear('userName');
                    Session::clear('userPwd');
                }
            }

            $data = [
                ['account', '=', $param['account']],
                ['password', '=', md5($param['password'])]
            ];

            $res = Db::table('admin_user')
                ->where($data)
                ->find();
            if (empty($res)) {
                $this->json('登录失败', null, 400);
                exit();
            }

            Session::set('remenber_login', $res['id']);
            $this->json('登录成功');
        }
    }

    public function register()
    {
        if (Request::isGet()) {
            $this->_view->render();
        } else if (Request::isPost()) {
            $param = Request::post();
            if (empty($param['account'])) {
                $this->json('用户名不能为空', null, 400);
                exit();
            }
            if (empty($param['password'])) {
                $this->json('密码不能为空', null, 400);
                exit();
            }
            if (empty($param['un_password'])) {
                $this->json('确认密码不能为空', null, 400);
                exit();
            }
            if ($param['un_password'] != $param['password']) {
                $this->json('两次密码不相等', null, 400);
                exit();
            }

            $res = Db::table('admin_user')
                ->where('account', $param['account'])
                ->find();
            if (!empty($res)) {
                $this->json('用户已存在', null, 400);
                exit();
            }

            $data = [
                'account' => $param['account'],
                'password' => md5($param['password']),
            ];

            $res = Db::table('admin_user')
                ->insert($data);
            if ($res > 0) {
                $this->json('注册成功');
                exit();
            }
        }
    }
}